using UnityEngine;

public class TeamUser : Team
{
	public Player player_control;

	public Transform transCursor_Control;

	public Transform transCursor_Pass;

	public Transform transCursor_Star;

	public Color color_cursor_control = new Color(0f, 0.5882353f, 1f, 1f);

	public Color color_cursor_pass_alpha = new Color(0f, 0.5882353f, 1f, 0.5f);

	public Color color_cursor_pass = new Color(0f, 0.5882353f, 1f, 1f);

	public Color color_cursor_star = Color.white;

	public UISprite spriteShoot;

	public UISprite spritePass;

	public UISprite spriteArrow;

	public bool isUpDownUping;

	private Transform transCursor;

	private void Start()
	{
		spriteShoot.spriteName = "sprite_game_block";
		spritePass.spriteName = "sprite_game_switch";
		spriteShoot.MakePixelPerfect();
		spritePass.MakePixelPerfect();
		transCursor_Control.gameObject.active = false;
		transCursor_Pass.gameObject.active = false;
		transCursor_Star.gameObject.active = false;
		changeCursor(transCursor_Pass, color_cursor_pass);
	}

	private new void FixedUpdate()
	{
		base.FixedUpdate();
		if (player_control == null)
		{
			spriteArrow.gameObject.SetActiveRecursively(state: false);
			if (transCursor != null)
			{
				transCursor.gameObject.SetActiveRecursively(state: false);
			}
			return;
		}
		Vector3 vector = Instance<CameraController>.instance.cameraMain.WorldToScreenPoint(player_control.getPosition());
		if (vector.x < 0f && Instance<UIGame>.instance.currentWidget == Instance<UIGame>.instance.widgetPlaying)
		{
			if (!spriteArrow.gameObject.active)
			{
				spriteArrow.gameObject.SetActiveRecursively(state: true);
			}
			spriteArrow.transform.localPosition = new Vector3(-370f * MyScreen.factor, Mathf.Clamp(vector.y - (float)Screen.height * 0.5f, -20f, (float)Screen.height * 0.5f - 20f), 0f);
			spriteArrow.flip = UIBasicSprite.Flip.Nothing;
		}
		else if (vector.x > (float)Screen.width && Instance<UIGame>.instance.currentWidget == Instance<UIGame>.instance.widgetPlaying)
		{
			if (!spriteArrow.gameObject.active)
			{
				spriteArrow.gameObject.SetActiveRecursively(state: true);
			}
			spriteArrow.flip = UIBasicSprite.Flip.Horizontally;
			spriteArrow.transform.localPosition = new Vector3(370f * MyScreen.factor, Mathf.Clamp(vector.y - (float)Screen.height * 0.5f, -20f, (float)Screen.height * 0.5f - 20f), 0f);
		}
		else if (spriteArrow.gameObject.active)
		{
			spriteArrow.gameObject.SetActiveRecursively(state: false);
		}
		if (player_control.isRootMotion)
		{
			transCursor.position = MathHelper.setY(player_control.trans_RootBone.position, 0.04f);
		}
		else
		{
			setTransCursor_Control();
		}
		if (isOffense)
		{
			transCursor.gameObject.active = true;
			transCursor.eulerAngles = player_control.getEulerAngles() + new Vector3(270f, 0f, 0f);
			if (getPlayer_ControlBall() == getPlayer(LoadData.teamDatas[UIChooseTeam.indexInTeamData_User].data_star - 1) || UIChooseTeam.indexInTeamData_User > 29)
			{
				changeCursor(transCursor_Star, color_cursor_star);
			}
			else
			{
				changeCursor(transCursor_Control, color_cursor_control);
			}
			if (getPlayer_ArrowTarget() != null && player_control.isControlBall)
			{
				transCursor_Pass.gameObject.active = true;
				transCursor_Pass.position = MathHelper.setY(getPlayer_ArrowTarget().getPosition(), 0.1f);
				transCursor_Pass.GetComponent<Renderer>().material.color = color_cursor_pass_alpha;
			}
			else
			{
				transCursor_Pass.gameObject.active = false;
			}
			return;
		}
		transCursor.gameObject.active = true;
		if (getPlayer_ControlBall() == getPlayer(LoadData.teamDatas[UIChooseTeam.indexInTeamData_User].data_star - 1) || UIChooseTeam.indexInTeamData_User > 29)
		{
			changeCursor(transCursor_Star, color_cursor_star);
			transCursor_Pass.gameObject.active = false;
		}
		else
		{
			changeCursor(transCursor_Pass, color_cursor_pass);
		}
		if (Instance<Ball>.instance.isInState(BallState_Dribble_Stay.getInstance()) && player_control.getParaTeam().IsAnyOneControlBall() && MathHelper.getSqrDistance(player_control.getPosition(), player_control.getParaTeam().getPlayer_ControlBall().getPosition()) < 2f)
		{
			spriteShoot.spriteName = "sprite_game_steal";
			spriteShoot.MakePixelPerfect();
			//if (TutorialController.preTutorialType == TutorialType.Game_Steal && player_control.isInState(PlayerStateBehavior_Run.getInstance()))
			//{
			//	player_control.do_idle();
			//	Instance<UIGame>.instance.isLockPauseBtn = true;
			//	Instance<UIGame>.instance.isLockPassBtn = true;
			//	Instance<UIGame>.instance.isLockShootBtn = false;
			//	Instance<UIGame>.instance.isLockJoystick = true;
			//	Instance<UIGame>.instance.joystick.resetToCenterPos();
			//	Instance<UIGame>.instance.showTutorialHandClickShoot();
			//	Instance<UIGame>.instance.showTutorialLabel("Tap steal button to steal the ball");
			//	if (Instance<Court>.instance.ojTutorialCircleTip != null)
			//	{
			//		Instance<Court>.instance.ojTutorialCircleTip.SetActiveRecursively(state: false);
			//		UnityEngine.Object.Destroy(Instance<Court>.instance.ojTutorialCircleTip);
			//	}
			//	Instance<Court>.instance.ojTutorialCircleTip = null;
			//}
		}
		else
		{
			spriteShoot.spriteName = "sprite_game_block";
			if (Instance<Court>.instance.isInState(CourtStateFreeThrow.getInstance()) && Instance<Court>.instance.foulTeamType == TeamType.COMPUTER)
			{
				spriteShoot.spriteName = "sprite_game_shoot";
			}
			spriteShoot.MakePixelPerfect();
		}
	}

	public void setTransCursor_Control()
	{
		transCursor.position = MathHelper.setY(player_control.getPosition(), 0.04f);
	}

	private void changeCursor(Transform trans, Color color)
	{
		if (!(transCursor != null) || !(transCursor == trans))
		{
			if (transCursor != null)
			{
				transCursor.gameObject.SetActiveRecursively(state: false);
				trans.position = transCursor.position;
			}
			transCursor = trans;
			transCursor.gameObject.SetActiveRecursively(state: true);
			transCursor.GetComponent<Renderer>().material.color = color;
		}
	}

	public void onDragStart()
	{
	}

	public void onDragging(float speed, Vector3 direction)
	{
		if (Instance<UIGame>.instance.isLockJoystick || player_control == null)
		{
			return;
		}
		if (isOffense && player_control.isControlBall)
		{
			if (!isUpDownUping)
			{
				player_control.do_dribble_run();
			}
		}
		else
		{
			player_control.do_run();
		}
		Vector3 position = player_control.getPosition();
		if (!isUpDownUping)
		{
			GameHelper.MoveTo(player_control, player_control.getPosition() + direction, player_control.getMoveSpeed(), isLookAtTargetPos: true);
		}
		if (player_control.paraPlayer != null && DSS.IsSomeOneFront(player_control.transform, player_control.paraPlayer.team.getPlayer_ClosestToTarget(player_control.getPosition()).getPosition()))
		{
			if (isOffense && player_control.paraPlayer.isInState(PlayerStateBehavior_Defense.getInstance()))
			{
				player_control.paraPlayer.do_defense_back();
			}
			player_control.dribblerun_is_charging = true;
			player_control.setPositon(position);
		}
		else if (player_control.paraPlayer != null)
		{
			player_control.dribblerun_is_charging = false;
		}
		//Instance<Court>.instance.tutorial.do_tutorial(TutorialType.Game_Pass);
	}

	public void onDragEnd()
	{
		if (Instance<UIGame>.instance.isLockJoystick || player_control == null)
		{
			return;
		}
		if (isOffense && player_control.isControlBall)
		{
			if (!isUpDownUping)
			{
				player_control.do_dribble_stay();
			}
		}
		else if (!player_control.isInState(PlayerStateBehavior_Shoot.getInstance()))
		{
			player_control.do_idle();
		}
		isUpDownUping = false;
	}

	public void onClickShoot()
	{
		if (isOffense)
		{
			if (player_control.isControlBall)
			{
				player_control.do_shoot();
			}
		}
		else if (Instance<Court>.instance.isInState(CourtStateFreeThrow.getInstance()))
		{
			if (Instance<Court>.instance.count_FreeThrow != 0 && Instance<Ball>.instance.isInState(BallState_Dribble_Stay.getInstance()))
			{
				player_control.do_freethrow();
			}
		}
		else if (!Instance<Ball>.instance.isJumpBalled)
		{
			player_control.do_jumpball();
		}
		else if (player_control.IsCanBlock())
		{
			player_control.do_block();
		}
		else if (Instance<Ball>.instance.isInState(BallState_Rebound.getInstance()) || Instance<Ball>.instance.isInState(BallState_ReboundInHand.getInstance()))
		{
			player_control.do_rebound();
		}
		else if (Instance<Ball>.instance.isInState(BallState_Dribble_Stay.getInstance()) && player_control.getParaTeam().IsAnyOneControlBall() && MathHelper.getSqrDistance(player_control.getPosition(), player_control.getParaTeam().getPlayer_ControlBall().getPosition()) < 2f && Instance<Court>.instance.isInState(CourtStatePlaying.getInstance()))
		{
			player_control.do_steal();
		}
		else
		{
			player_control.do_jump();
		}
	}

	public void onClickPass()
	{
		if (isOffense)
		{
			if (player_control.isControlBall)
			{
				player_control.do_pass();
			}
			return;
		}
		Player player = player_control;
		setPlayer_ControlBall(getPlayer_Switch());
		player_control.do_idle();
		if (Instance<Court>.instance.isInState(CourtStateFreeThrow.getInstance()))
		{
			player_control.AI.do_defense_freethrow();
			return;
		}
		player.AI.time_Stop = 0f;
		player.AI.do_defense_defense();
	}

	public void onUpDownUp()
	{
		if (!isUpDownUping && isOffense && player_control.isControlBall)
		{
			isUpDownUping = true;
			player_control.do_updownup();
		}
	}

	public override Player getPlayer_ControlBall()
	{
		return player_control;
	}

	public void setPlayer_ControlBall(Player player)
	{
		if (player_control != null && player_control.ojShadow != null)
		{
			player_control.ojShadow.SetActiveRecursively(state: true);
		}
		player_control = player;
		if (player_control != null && player_control.ojShadow != null)
		{
			player_control.ojShadow.SetActiveRecursively(state: false);
		}
	}

	public override Player getPlayer_HelpDefense()
	{
		if (base.getPlayer_HelpDefense() == getPlayer_ControlBall())
		{
			return getPlayer_ClosestTeammate(getPlayer_ControlBall());
		}
		return base.getPlayer_HelpDefense();
	}

	public override void OnJumpBall()
	{
		base.OnJumpBall();
		setPlayer_ControlBall(playerC);
		spriteShoot.spriteName = "sprite_game_block";
		spritePass.spriteName = "sprite_game_switch";
		spriteShoot.MakePixelPerfect();
		spritePass.MakePixelPerfect();
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player_control)
			{
				getPlayer(i).OnJumpBall();
			}
		}
		player_control.do_idle();
		player_control.LookAt(MathHelper.setY(Instance<CameraController>.instance.getCameraPos(), 0f));
	}

	public override void OnBaseLine(Vector3 targetPos)
	{
		base.OnBaseLine(targetPos);
		Player player = getPlayer(PlayerType.POINTGUARD);
		if (player == player_control)
		{
			player = getPlayer(PlayerType.SHOOTINGGUARD);
		}
		Player player2 = getPlayer(PlayerType.CENTER);
		if (player2 == player_control)
		{
			player2 = getPlayer(PlayerType.POWERFORWARD);
		}
		player2.AI.do_baseline(targetPos);
		player.acceptbasslineball_targetPos = getAccetpBaseLineBallPos(targetPos);
		player.AI.do_acceptbaselineball();
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player && getPlayer(i) != player2)
			{
				if (getPlayer(i) != player_control)
				{
					getPlayer(i).AI.do_offense_findpos();
				}
				else if (!player_control.isInState(PlayerStateBehavior_Tumble.getInstance()))
				{
					getPlayer(i).do_idle();
				}
			}
		}
		spriteShoot.spriteName = "sprite_game_shoot";
		spritePass.spriteName = "sprite_game_pass";
		spriteShoot.MakePixelPerfect();
		spritePass.MakePixelPerfect();
	}

	public override void OnParaBaseLine()
	{
		base.OnParaBaseLine();
		spriteShoot.spriteName = "sprite_game_block";
		spritePass.spriteName = "sprite_game_switch";
		spriteShoot.MakePixelPerfect();
		spritePass.MakePixelPerfect();
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player_control)
			{
				getPlayer(i).AI.time_Stop = 0f;
				getPlayer(i).AI.do_defense_defense();
			}
		}
	}

	public override void OnParaBallIn()
	{
		base.OnParaBallIn();
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player_control)
			{
				getPlayer(i).AI.do_idle();
			}
		}
	}

	public override void OnBallIn()
	{
		base.OnBallIn();
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player_control)
			{
				getPlayer(i).AI.do_defense_defense();
			}
		}
	}

	public override void OnGetBall(Player player)
	{
		base.OnGetBall(player);
		spriteShoot.spriteName = "sprite_game_shoot";
		spritePass.spriteName = "sprite_game_pass";
		spriteShoot.MakePixelPerfect();
		spritePass.MakePixelPerfect();
		setPlayer_ControlBall(player);
		player_control.do_dribble_stay();
	}

	public override void setIsOffense(bool _isOffense)
	{
		base.setIsOffense(_isOffense);
		if (_isOffense)
		{
			spriteShoot.spriteName = "sprite_game_shoot";
			spritePass.spriteName = "sprite_game_pass";
			spriteShoot.MakePixelPerfect();
			spritePass.MakePixelPerfect();
		}
	}

	public override void OnParaGetBall()
	{
		base.OnParaGetBall();
		spriteShoot.spriteName = "sprite_game_block";
		spritePass.spriteName = "sprite_game_switch";
		spriteShoot.MakePixelPerfect();
		spritePass.MakePixelPerfect();
		player_control.do_defense();
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player_control)
			{
				getPlayer(i).AI.time_Stop = 0f;
				getPlayer(i).AI.do_defense_defense();
			}
		}
	}

	public override void OnShoot(Player player)
	{
		base.OnShoot(player);
		Vector3 target = Global.posBallIn_Right_Ground;
		if (offenseDirection == OffenseDirection.LEFT)
		{
			target = Global.posBallIn_Left_Ground;
		}
		Player player_ClosestToTarget = getPlayer_ClosestToTarget(target);
		if (player_ClosestToTarget != player_control)
		{
			player_ClosestToTarget.AI.do_offense_grabrebound();
		}
		else
		{
			getPlayer_ClosestTeammate(player).AI.do_offense_grabrebound();
		}
		Vector3 target2 = (offenseDirection != OffenseDirection.RIGHT) ? Global.posBallIn_Right_Ground : Global.posBallIn_Left_Ground;
		Player player_ClosestToTarget2 = getPlayer_ClosestToTarget(target2);
		if (player_ClosestToTarget2 != player_control)
		{
			player_ClosestToTarget2.AI.time_Stop = 0f;
			player_ClosestToTarget2.AI.do_defense_defense();
		}
	}

	public override void OnParaShoot()
	{
		base.OnParaShoot();
		bool flag = false;
		PlayerType playerType = PlayerType.CENTER;
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player_control && getPlayer(i).IsCanBlock())
			{
				getPlayer(i).AI.do_defense_block();
				flag = true;
				playerType = getPlayer(i).playerType;
				break;
			}
		}
		Vector3 target = (offenseDirection != 0) ? Global.posBallIn_Left_Ground : Global.posBallIn_Right_Ground;
		if (!flag)
		{
			Player player_ClosestToTarget = getPlayer_ClosestToTarget(target);
			if (player_ClosestToTarget != player_control)
			{
				player_ClosestToTarget.AI.do_defense_grabrebound();
				if (getPlayer_ClosestTeammate(player_ClosestToTarget) != player_control)
				{
					getPlayer_ClosestTeammate(player_ClosestToTarget).AI.do_defense_grabrebound();
				}
				else
				{
					getPlayer_ClosestTeammate(player_control).AI.do_defense_grabrebound();
				}
			}
			else
			{
				getPlayer_ClosestTeammate(player_control).AI.do_defense_grabrebound();
			}
		}
		else
		{
			Player player_ClosestToTarget2 = getPlayer_ClosestToTarget(target);
			if (player_ClosestToTarget2.playerType != playerType && player_ClosestToTarget2 != player_control)
			{
				player_ClosestToTarget2.AI.do_defense_grabrebound();
			}
			if (getPlayer_ClosestTeammate(player_ClosestToTarget2) != player_control)
			{
				getPlayer_ClosestTeammate(player_ClosestToTarget2).AI.do_defense_grabrebound();
			}
		}
	}

	public override void OnFreeThrow()
	{
		base.OnFreeThrow();
		setIsAllAI(_isAllAI: true);
	}

	public override void OnParaFreeThrow()
	{
		base.OnParaFreeThrow();
		setIsAllAI(_isAllAI: true);
	}

	public override Player getPlayer_CatchPass(Player playerPass)
	{
		if (getPlayer_ArrowTarget() != null)
		{
			return getPlayer_ArrowTarget();
		}
		return base.getPlayer_CatchPass(playerPass);
	}

	private Player getPlayer_Switch()
	{
		if (Instance<Court>.instance.isInState(CourtStateFreeThrow.getInstance()))
		{
			return getPlayer((int)(player_control.playerType + 1) % 5);
		}
		Player player = player_control;
		float num = float.PositiveInfinity;
		if (offenseDirection == OffenseDirection.RIGHT)
		{
			for (int i = 0; i < Team.PlayerCount; i++)
			{
				Vector3 position = getPlayer(i).getPosition();
				float z = position.z;
				Vector3 position2 = Instance<Ball>.instance.getPosition();
				if (z > position2.z)
				{
					float sqrDistance = MathHelper.getSqrDistance(getPlayer(i).getPosition(), Instance<Ball>.instance.getPosition());
					if (sqrDistance < num)
					{
						player = getPlayer(i);
						num = sqrDistance;
					}
				}
			}
		}
		else
		{
			for (int j = 0; j < Team.PlayerCount; j++)
			{
				Vector3 position3 = getPlayer(j).getPosition();
				float z2 = position3.z;
				Vector3 position4 = Instance<Ball>.instance.getPosition();
				if (z2 < position4.z)
				{
					float sqrDistance2 = MathHelper.getSqrDistance(getPlayer(j).getPosition(), Instance<Ball>.instance.getPosition());
					if (sqrDistance2 < num)
					{
						player = getPlayer(j);
						num = sqrDistance2;
					}
				}
			}
		}
		if (player == player_control)
		{
			player = getPlayer_ClosestToTarget(Instance<Ball>.instance.getPosition());
			if (player == player_control)
			{
				player = getPlayer_ClosestTeammate(player_control);
			}
		}
		return player;
	}

	private Player getPlayer_ArrowTarget()
	{
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			if (getPlayer(i) != player_control)
			{
				Vector3 from = getPlayer(i).getPosition() - player_control.getPosition();
				if (Vector3.Angle(from, player_control.transform.forward) < 50f && Vector3.Angle(from, player_control.transform.forward) > -50f)
				{
					return getPlayer(i);
				}
			}
		}
		return null;
	}
}
